package Interfaz;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;

public class EditorTexto extends JTextArea {

    private int lineCount;
    private Font font;
    private int fontHeight;
    private int fontWidth;
    private int x,  y;

    public EditorTexto() {
        font = getFont();
        lineCount = 1;
        fontHeight = getFontMetrics(getFont()).getHeight();
        fontWidth = fontHeight - 10;
        y = 1280;
        x = 5 + (int) (Math.log10(lineCount) + 1) * (fontWidth) + 5;
        addKeyListener(new Adapter());
        setMargin(new Insets(0, x + 10, 0,0 ));
    }

    private void actualizar() {
        lineCount = getLineCount();
        y = (int) getSize().getHeight();
        x = 5 + (int) (Math.log10(lineCount) + 1) * (fontWidth) + 5;
        repaint();
    }

    private class Adapter extends KeyAdapter {

        @Override
        public void keyTyped(KeyEvent e) {
            actualizar();
        }
    }

    public void setText(String cadena) {
        super.setText(cadena);
        actualizar();
    }

    public void paint(Graphics g) {
        super.paint(g);
        font.deriveFont(fontHeight);
        g.setFont(font);
        g.setColor(Color.lightGray);
        g.drawRect(-1, -1, x, y + fontHeight + 2);
        g.setColor(new Color(233, 232, 226));
        g.fillRect(-1, -1, x, y + fontHeight + 2);
        g.setColor(Color.black);
        for (int i = 0; i <= lineCount; i++) {
            g.drawString("" + i, x - (int) (Math.log10(i) + 1) * (fontWidth) - 5,3+ i * (fontHeight-1) );
        }
    }
}
